﻿using System;
using System.Net.Http;
using Weapp.Data.Entities;
using Weapp.Data.Enums;

namespace Weapp.Web.Models
{
    public class ModelFactory
    {
        private readonly System.Web.Http.Routing.UrlHelper _urlHelper;

        public ModelFactory()
        {

        }

        public ModelFactory(HttpRequestMessage request)
        {
            _urlHelper = new System.Web.Http.Routing.UrlHelper(request);
        }

        public SizeCategoryModel Create(SizeCategory sizeCategory)
        {
            return new SizeCategoryModel()
            {
                Url = _urlHelper.Link("SizeCategory", new { id = sizeCategory.SizeCategoryId }),
                AttackAndArmorClassModifier = sizeCategory.AttackAndArmorClassModifier,
                BipedCarryingCapacityMultiplier = sizeCategory.BipedCarryingCapacityMultiplier,
                MaxHeightOrLength = sizeCategory.MaxHeightOrLength,
                MaxWeight = sizeCategory.MaxWeight,
                MinHeightOrLength = sizeCategory.MaxHeightOrLength,
                MinWeight = sizeCategory.MinWeight,
                Name = sizeCategory.Name,
                NaturalReachLong = sizeCategory.NaturalReachLong,
                NaturalReachTall = sizeCategory.NaturalReachTall,
                QuadrupedCarryingCapacityMultiplier = sizeCategory.QuadrupedCarryingCapacityMultiplier,
                SizeCategoryId = sizeCategory.SizeCategoryId,
                Space = sizeCategory.Space,
                SpecialAttacksModifier = sizeCategory.SpecialAttacksModifier
            };
        }

        public SizeCategory Parse(SizeCategoryModel model)
        {
            try
            {
                var sizeCategory = new SizeCategory()
                {
                    AttackAndArmorClassModifier = model.AttackAndArmorClassModifier,
                    BipedCarryingCapacityMultiplier = model.BipedCarryingCapacityMultiplier,
                    MaxHeightOrLength = model.MaxHeightOrLength,
                    MaxWeight = model.MaxWeight,
                    MinHeightOrLength = model.MaxHeightOrLength,
                    MinWeight = model.MinWeight,
                    Name = model.Name,
                    NaturalReachLong = model.NaturalReachLong,
                    NaturalReachTall = model.NaturalReachTall,
                    QuadrupedCarryingCapacityMultiplier = model.QuadrupedCarryingCapacityMultiplier,
                    SizeCategoryId = model.SizeCategoryId,
                    Space = model.Space,
                    SpecialAttacksModifier = model.SpecialAttacksModifier
                };

                return sizeCategory;
            }
            catch (Exception)
            {
                return null;
            }
        }


        public UserDnD Parse(UserDnDModel model)
        {
            try
            {
                var UserDnD = new UserDnD()
                {
                    Account = model.Account,
                    UserName = model.UserName,
                    Password = model.Password,
                    Role = (model.Role == "Master")?Role.Master:Role.Player
                };

                return UserDnD;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public UserDnDModel Create(UserDnD userDnD)
        {
            return new UserDnDModel()
            {
                //Url = _urlHelper.Link("SizeCategory", new { id = userDnD.Account }),
                Account = userDnD.Account,
                UserName = userDnD.UserName,
                Password = userDnD.Password,
                Role = userDnD.Role.ToString("G")
            };
        }
    }
}